<template>
    <view class="data-box">
        <!-- 列表头部 -->
        <view class="data-fixed-box">
            <!-- 搜索 -->
            <search
                @searchText="searchText"
                :placeholder="placeholder"
                :showAddGoods="showAddGoods"
                :md="cggys"
                @openObjTypeList="openObjTypeList"
                @searchAction="searchAction"
                @inputSearchKw="inputSearchKw"
                @inputFocus="inputFocus"
                @setTitleValue="setTitleValue"
                @upDownClick="upDownClick"
                @addSupplier="addSupplier"
                ref="search"
            >
            </search>

            <!-- 供应商过滤 -->
            <customFiters
                v-show="isShowFilters && !isSearching"
                :topVal="custFilterTopVal"
                :limitViewOtherCustom="limitViewOtherCustom"
                @setCustCate="setCustCate"
                @setSortId="setSortId"
                @setUcpId="setUcpId"
                @groupYunTagProCityAreaFilter="groupYunTagProCityAreaFilter"
                ref="customFiter"
            >
            </customFiters>

            <!-- 搜索类型下拉弹出 -->
            <searchTotal :total="total" v-show="isSearching"></searchTotal>

            <!-- 搜索类型下拉弹出 -->
            <searchPop
                ref="searchPop"
                :type="searchGdAndCustType"
                @changeObjType="changeObjType"
                @showAndHideTypeList="showAndHideTypeList"
                @setDefaultObjType="setDefaultObjType"
            >
            </searchPop>
            <!-- 关键字搜索结果 -->
            <searchKwResult
                ref="searchKwResult"
                @doSearch="doSearch"
            ></searchKwResult>
        </view>

        <!-- 供应商列表 -->
        <supplierList
            :list="suppList"
            ref="setSuppplierId"
            :md="cggys"
            :isShowName="isShowName"
            :isShowCp="isShowCp"
            @getSuppList="getSuppList"
            @searchSuppCatAndKwList="searchSuppCatAndKwList"
            @setSuppmInfo="setSuppInfoData"
            :topValue="suppListTopval"
            :isSearching="isSearching"
        >
        </supplierList>
    </view>
</template>

<script>
import supplierList from "../comm/SupplierList.vue";

import search from "../comm/Search.vue";

import filters from "../goods/Goodsfilters.vue";

import customFiters from "../comm/CustomFilters.vue";

import customList from "../comm/CustomList.vue";

import staticConst from "../../js/const/StaticConst.js";

import config from "../../js/conf/Config.js";

import eventCtrl from "../../js/event/EventCtrl.js";

import https from "../../js/https/Https.js";

import accessCtrl from "../../js/limit/AccessCtrl.js";

import lang from "../../js/conf/Lang.js";

import searcher from "../../js/search/Searcher.js";

import searchPop from "../comm/SearchPop.vue";

import searchKwResult from "../comm/SearchKwResult.vue";

import searchTotal from "../comm/SearchTotal.vue";

import supplierCtrl from "../../js/supplier/SupplierCtrl.js";

import localStore from "../../js/LocalStore.js";

let pageSize = staticConst.PAGE_SIZE;
let custPage = 0;
export default {
    components: {
        search,
        filters,
        customFiters,
        searchPop,
        searchTotal,
        searchKwResult,
        customList,
        supplierList,
    },
    data() {
        return {
            suppListTopval: 150, //供应商列表距离顶部的高度
            seeCustom: false,
            suppListTopval: 110, //供应商列表距离顶部的高度
            custFilterTopVal: 90, //供应商过滤距离顶部的高度
            cggys: staticConst.CG_GYS, //供应商列表详情模块
            isShowFilters: true, //是否显示过滤栏
            placeholder: "搜索供应商",
            BoxHeight: false,
            showAddGoods: false,
            isSearching: false,
            objType: null,
            keyWord: "",
            total: 0,
            exactKw: null,

            //加载供应商列表
            suppData: {
                typeid: "all", //供应商类型
                orderby: "", //排序 1 创建时间 2 欠款额度 3 交易单数 4最近交易
                uid_cp: "", //	经手人
                group: "", //	非必须 筛选 1 30天内没消费供应商 2 30天内新增供应商
                idyun: "", //	云店id 非筛选项 不传该参数
                isyun: "", //	是否为云店供应商 1是其他 否 非筛选项 不传该参数
                is_yy: "", //是否为预约 1是 其他否 默认否
                pfrom: 0, //	开始id 非必须 默认为0
                pnum: pageSize, //每页数量 非必须 默认0
                province: "", //	省份搜索 int 省份code 非筛选项 不传该参数
                city: "", //城市搜索 int 市code 非筛选项 不传该参数
                area: "", //区域搜索 int 区域code 非筛选项 不传该参数
                customerid: "", //搜索供应商id
                tagid: "", //标签id 非筛选项 不传该参数
            },

            suppList: [], //供应商列表
            searchGdAndCustType: 4, //1:产品，2：单据， 3：供应商 4：供应商

            customerId: 0, //供应商id；
            isCheckOut: false, //是否是结算

            limitViewOtherCustom: true,
            isLoading: false,

            mySelf: null,

            isShowCp: true, //是否显示供应商公司名称
            isShowName: true, //是否显示供应商名称
            isoll: false,
        };
    },

    methods: {
        /***初始化列表***/
        async initData() {
            this.seeCustom = await accessCtrl.getLimitSeeCustomer();
            this.mySelf = await localStore.getUserInfo();
            this.limitViewOtherCustom =
                await accessCtrl.getLimitViewOtherCustom();
            this.showAddGoods = await accessCtrl.getGdsAddLimit();
            this.resetSeachCustData();
            this.clearCustomList();
            await this.getSuppList();
            await searcher.loadGdsSchObjTypeLs(searcher.CH_SUPP);
        },

        resetSeachCustData() {
            this.suppData = {
                typeid: "all", //供应商类型
                orderby: "", //排序 1 创建时间 2 欠款额度 3 交易单数 4最近交易
                uid_cp: "", //	经手人
                group: "", //	非必须 筛选 1 30天内没消费供应商 2 30天内新增供应商
                idyun: "", //	云店id 非筛选项 不传该参数
                isyun: "", //	是否为云店供应商 1是其他 否 非筛选项 不传该参数
                is_yy: "", //是否为预约 1是 其他否 默认否
                pfrom: 0, //	开始id 非必须 默认为0
                pnum: pageSize, //每页数量 非必须 默认0
                province: "", //	省份搜索 int 省份code 非筛选项 不传该参数
                city: "", //城市搜索 int 市code 非筛选项 不传该参数
                area: "", //区域搜索 int 区域code 非筛选项 不传该参数
                customerid: "", //搜索供应商id
                tagid: "", //标签id 非筛选项 不传该参数
            };
        },

        /*********/
        setGdCustomIdNull() {
            this.$refs.setSuppplierId.setGdSuppIdNull();
        },

        /****分类筛选****/
        setCustCate(id) {
            this.clearCustomList();
            this.suppData.typeid = id;
            this.getSuppList();
        },

        /***排序赛选***/
        setSortId(id) {
            this.clearCustomList();
            this.suppData.orderby = id;
            this.getSuppList();
        },

        /****经手人筛选****/
        setUcpId(id) {
            this.clearCustomList();
            this.suppData.uid_cp = id;
            this.getSuppList();
        },

        /*** 筛选智能 ,云店，标签，省份，城市，区域***/
        groupYunTagProCityAreaFilter(data) {
            this.clearCustomList();
            this.suppData.province = data.province;
            this.suppData.area = data.area;
            this.suppData.idyun = data.idyun;
            this.suppData.tagid = data.tagid;
            this.suppData.group = data.group;
            this.getSuppList();
            this.setListBoxHeight(data.len > 0);
        },

        /****选择供应商****/
        setSuppInfoData(item, index) {
            this.$emit("setSuppmInfo", item, this.isCheckOut);
        },

        /***添加供应商*****/
        addNewCustomer() {
            this.clearCustomList();
            this.getSuppList();
        },

        /***编辑供应商****/
        updataCustomList(data) {
            let len = this.suppList.length;
            for (let i = 0; i < len; i++) {
                let temp = this.suppList[i];
                if (temp.id == data.id) {
                    temp.avatar = data.avatar;
                    temp.tag = data.tag;
                    temp.cellphone = data.cellphone;
                    temp.name = data.name;
                    temp.company = data.company;
                    temp.isold = data.isold;
                    temp.isold_name = data.isold_name;
                    break;
                }
            }
            this.$forceUpdate();
        },

        /***供应商列表***/
        getSuppList() {
            let that = this;
            if (this.isLoading || this.isoll) return;

            if (!this.limitViewOtherCustom && this.mySelf) {
                this.suppData.uid_cp = this.mySelf.uid;
            }

            this.isLoading = true;
            this.suppData.pfrom = custPage * pageSize;
            supplierCtrl.getSuppList(this.suppData, back);
            function back(data) {
                that.isLoading = false;
                if (!data.data) return;
                data = data.data;
                supplierCtrl.setIsShowCp(data.is_cp);
                supplierCtrl.setIsShowName(data.is_name);

                that.isShowCp = supplierCtrl.getIsShowCp();
                that.isShowName = supplierCtrl.getIsShowName();

                if (data.supp_ls && data.supp_ls.length > 0) {
                    that.suppList = that.suppList.concat(data.supp_ls);
                    if (
                        custPage == 0 &&
                        that.suppList &&
                        that.suppList.length > 0
                    ) {
                        that.setSuppmInfo(that.suppList[0]);
                        that.$refs.setSuppplierId.setCustomId(
                            that.suppList[0].id
                        );
                    }

                    custPage++;
                } else {
                    that.isoll = true;
                }

                let obj = {};
                if (data.custype_ls) {
                    data.custype_ls.unshift({ id: "all", title: "全部" });
                    data.custype_ls.push({ id: "0", title: "未分类" });
                    obj.cateList = data.custype_ls;
                }

                if (data.order_ls) {
                    let len = data.order_ls.length;
                    for (let i = 0; i < len; i++) {
                        let temp = data.order_ls[i];
                        if (temp.is_ck == 1) {
                            obj.customSortCheckId = temp.id;
                            break;
                        }
                    }
                    obj.customSortList = data.order_ls;
                }

                if (data.group_ls) obj.mindLs = data.group_ls;
                obj.isHandlers = false;
                that.$refs.customFiter.initData(obj);
            }
        },

        /****选中供应商******/
        setSuppmInfo(data) {
            if (data) this.$emit("setSuppmInfo", data);
        },

        clearCustomList() {
            custPage = 0;
            this.isoll = false;
            this.suppList = [];
        },

        /***弹出搜索类型列表***/
        openObjTypeList(value) {
            this.searchAction(true);
            if (value) {
                this.$refs.searchPop.show("show");
                if (this.objType)
                    this.placeholder = "请输入" + this.objType.name;
            } else {
                this.$refs.searchPop.show();
            }
        },

        /***设置输入框，选择的搜索关键字值****/
        setTitleValue(item) {
            this.$refs.search.setCatSearchTxt(item.title);
        },

        /*****搜索产品****/
        doSearch(item, listType) {
            this.clearCustomList();
            let data = {};
            data.listType = listType;
            this.$refs.search.setCatSearchTxt(item.title);
            if (listType == 0) {
                data.exact = 1;
                data.kw = item.title;
            } else {
                data.exact = 2;
                data.kw = "";
                data.typeid = item.id;
            }
            this.exactKw = data;
            this.searchSuppCatAndKwList();
        },

        /****按分类或关键字搜索供应商****/
        searchSuppCatAndKwList() {
            if (!this.exactKw) return;
            if (this.isLoading) return;
            this.isLoading = true;
            let that = this;
            let obj = {};
            obj.exact = this.exactKw.exact;
            obj.kw = this.exactKw.kw;
            obj.typeid = this.exactKw.typeid;
            if (this.exactKw.listType == 0) {
                if (this.objType) obj.objtype = this.objType.code;
            } else {
                // obj.objtype = "";
                if (this.objType) obj.objtype = this.objType.code;
            }
            obj.pfrom = custPage * pageSize;
            obj.pnum = pageSize;
            https.searchSuppCatAndKwList(obj, callBack);
            function callBack(data) {
                that.isLoading = false;
                if (data && data.status == 1) {
                    if (!data.data) return;
                    data = data.data;
                    if (data.total) that.total = data.total;
                    if (!data.supp_ls) return;
                    that.suppList = that.suppList.concat(data.supp_ls);
                    custPage++;
                }
            }
        },

        /***是否搜索****/
        searchAction(value) {
            this.isSearching = value;
            if (!value) {
                this.$refs.searchPop.show();
                this.exactKw = null;
                this.keyWord = "";
                this.total = 0;
                this.$refs.search.clearSearchTxt();
                this.$refs.searchKwResult.setListData([]);
                this.$refs.searchKwResult.setListData("");
                this.clearCustomList();
                this.placeholder = "搜索供应商";
                this.getSuppList();
            }
            // this.checkOut(value);
            this.setCustLsTopval(value);
        },

        /***输入关键字搜索*****/
        inputSearchKw(kw) {
            let data = {};
            let that = this;
            this.keyWord = kw;
            data.kw = kw;
            if (this.objType) data.objtype = this.objType.code;
            data.ch = searcher.CH_SUPP;
            https.searchKwByObjType(data, callBack);
            function callBack(obj) {
                if (obj.status == 1) {
                    that.total = obj.data.total;
                    that.$refs.searchKwResult.setKeyWord(that.keyWord);
                    if (obj.data && obj.data.ls && obj.data.ls.length) {
                        that.$refs.searchKwResult.setListData(obj.data.ls);
                    } else {
                        that.$refs.searchKwResult.setListData([]);
                    }
                } else {
                    that.$refs.searchKwResult.setListData([]);
                }
            }
        },

        /*****输入框获取焦点处理****/
        inputFocus(inputTxt) {
            this.$refs.searchPop.init();
            if (this.objType) this.placeholder = "请输入" + this.objType.name;
            this.$refs.search.searchAction(true);
            if (inputTxt == "") {
                let list = searcher.getSuppCatList();
                let listType = 1; //listType 1:分类列表
                this.$refs.searchKwResult.setListData(list, listType);
            }
        },

        /***改变搜索类型***/
        changeObjType(item) {
            this.placeholder = "请输入" + item.name;
            this.objType = item;
            this.$refs.searchPop.show();
        },

        /****显示或隐藏类型列表****/
        showAndHideTypeList(value) {
            this.$refs.search.setIsOpenList(value);
        },

        /****初始化设置默认objType*****/
        setDefaultObjType(item) {
            this.objType = item;
        },

        /*** 根据筛选重新设置list盒子高度 ***/
        setListBoxHeight(data) {
            this.BoxHeight = data;
            if (data) this.suppListTopval = 146;
            else this.suppListTopval = 110;
        },

        /*** 搜索 ***/
        searchText(kw) {
            let item = this.getSelectedKwTitle();
            this.$refs.searchKwResult.setShowValue(false);

            this.clearCustomList();
            let data = {};
            if (item != null) {
                this.keyWord = item.title;
                data.exact = 1;
                this.$refs.search.setCatSearchTxt(item.title);
            } else {
                this.keyWord = kw;
                data.exact = 2;
            }
            data.listType = 0;
            data.kw = this.keyWord;
            this.exactKw = data;
            this.searchSuppCatAndKwList();
        },

        /*****获取选择的关键字****/
        getSelectedKwTitle() {
            let item = this.$refs.searchKwResult.getSelectedItem();
            return item;
        },

        /***注册时间***/
        addEvent() {
            uni.$on(eventCtrl.ACTION_GOODS_EVENT, this.actionHandler);
        },

        removeEvent() {
            uni.$off(eventCtrl.ACTION_GOODS_EVENT);
        },

        /****处理操作菜单***/
        actionHandler(data) {
            this.removeEvent();
            this.addEvent();
        },

        onClickPageHandler(event) {
            if (this.isSearching) return;
            let detail = event.detail;
            if (detail.x < 72 || detail.x > 437 || detail.y < 70) {
                this.$refs.filtersBox.onClickPageHandler(event);
            }
        },

        setCustLsTopval(value) {
            this.suppListTopval = value ? 110 : 110;
            this.custFilterTopVal = value ? 90 : 90;
        },

        /******删除供应商********/
        removeCustom(id) {
            console.log("suppList========:", this.suppList);
            let len = this.suppList.length;
            let idx = -1;
            for (let i = 0; i < len; i++) {
                let temp = this.suppList[i];
                console.log("");
                if (temp.id == id) {
                    idx = i;
                    this.suppList.splice(i, 1);
                    break;
                }
            }
            len = this.suppList.length;
            if (len > 0) {
                if (idx != -1) {
                    this.$refs.setSuppplierId.clickItem(this.suppList[idx]);
                }
            }
        },

        /****点击导入导出供应商****/
        upDownClick() {
            this.$emit("upDownClick");
        },

        /****添加供应商****/
        addSupplier() {
            this.$emit("addSupplier");
        },
    },
    mounted() {
        this.addEvent();
    },
};
</script>
<style lang="scss">
.data-box {
    position: fixed;
    margin-left: 0;
    box-sizing: border-box;
    width: 345px;
    height: 100vh;
    border-right: 1px solid #ececec;
    .data-fixed-box {
        width: 345px;
        box-sizing: border-box;
        border-right: 1px solid #ececec;
        position: fixed;
        top: 0;
        background: #fff;
        z-index: 1;
    }
}
</style>